package com.blackcat.starter.config;

import com.blackcat.starter.properties.BlackCatProperties;
import com.sun.net.httpserver.HttpServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * <p> 描述 : 自动配置类
 * 		创建 HttpServer Bean，并启动该服务器
 * @author : blackcat
 * @date  : 2020/7/27 14:31
 * @link http://www.iocoder.cn/Spring-Boot/autoconfigure/?vip 【芋道 Spring Boot 自动配置原理】 学习笔记
*/
@Configuration // 声明配置类
@EnableConfigurationProperties(BlackCatProperties.class) // 使 BlackCatProperties 配置属性类生效
public class BlackCatAutoConfiguration {

	private Logger logger = LoggerFactory.getLogger(BlackCatAutoConfiguration.class);

	@Bean // 声明创建 Bean
	@ConditionalOnClass(HttpServer.class) // 需要项目中存在 com.sun.net.httpserver.HttpServer 类。该类为 JDK 自带，所以一定成立。
	public HttpServer httpServer(BlackCatProperties serverProperties) throws IOException {
		// 创建 HttpServer 对象，并启动
		HttpServer server = HttpServer.create(new InetSocketAddress(serverProperties.getPort()), 0);
		server.start();
		logger.info("[httpServer][启动服务器成功，端口为:{}]", serverProperties.getPort());
		// 返回
		return server;
	}
}
